<!--
 * @Descripttion: 编辑轮播图
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-11-19 17:39:12
-->
<template>
  <div class="lb-malls-edit-banner">
    <top-nav :title="navTitle" :isBack="true"></top-nav>
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="bannerForm"
        :rules="bannerFormRules"
        ref="bannerForm"
        label-width="140px"
      >
        <el-form-item label="所属公司" prop="c_id">
          <el-select v-model="bannerForm.c_id" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <lb-cover
            :fileList="bannerForm.img"
            @selectedFiles="getImg"
          ></lb-cover>
          <lb-tool-tips>图片建议尺寸: 750 * 400</lb-tool-tips>
        </el-form-item>
        <el-form-item label="排序值" prop="top">
          <el-input-number
            class="lb-input-number"
            v-model="bannerForm.top"
            :controls="false"
            :precision="0"
            :min="0"
            placeholder="请输入排序值"
          ></el-input-number>
          <lb-tool-tips>值越大, 排序越靠前</lb-tool-tips>
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitFormInfo('bannerForm')">{{
            $t('action.submit')
          }}</lb-button>
          <lb-button @click="goBack">{{ $t('action.back') }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      navTitle: '',
      bannerForm: {
        c_id: '',
        img: [],
        top: ''
      },
      bannerFormRules: {
        c_id: { required: true, type: 'number', message: '请选择', trigger: 'change' },
        img: { required: true, type: 'array', message: '请上传图片', trigger: 'change' },
        top: { required: true, type: 'number', message: '请输入排序值', trigger: 'blur' }
      },
      options: []
    }
  },
  created () {
    let { id } = this.$route.query
    this.navTitle = this.$t(id ? 'menu.EditBanner' : 'menu.AddBanner')
    this.getCompanyList()
    if (id) this.getFormInfo(id)
  },
  methods: {
    getFormInfo (id) {
      this.$api.editUpdateMallsBanner({ id }).then(res => {
        if (res.code === 200) {
          this.bannerForm.c_id = res.data.c_id
          this.bannerForm.img = [{ url: res.data.img }]
          this.bannerForm.id = res.data.id
          this.bannerForm.top = res.data.top
        }
      })
    },
    getCompanyList () {
      this.$api.getMallsCompanyOptions().then(res => {
        this.options = res.data
      })
    },
    submitFormInfo (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let { bannerForm } = this
          let subForm = JSON.parse(JSON.stringify(bannerForm))
          subForm.img = bannerForm.img.map(item => { return item.url })
          if (bannerForm.id) {
            this.$api.editUpdateMallsBanner(subForm).then(res => {
              if (res.code === 200) {
                this.$message.success(this.$t('tips.successSub'))
                this.goBack()
              }
            })
          } else {
            this.$api.addMallsBanner(subForm).then(res => {
              if (res.code === 200) {
                this.$message.success(this.$t('tips.successSub'))
                this.goBack()
              }
            })
          }
        }
      })
    },
    getImg (img) {
      this.bannerForm.img = img
    },
    goBack () {
      this.$router.back(-1)
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-malls-edit-banner {
  width: 100%;
  .page-main {
    width: 100%;
    .el-input,
    .el-input-number,
    .el-select {
      width: 300px;
    }
  }
}
</style>
